/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.vineatoum.view.utils.jtablecustom;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class CutomTableModel<T extends TableListener> extends DefaultTableModel {
	private static final long serialVersionUID = 6693898254334365299L;

	Vector<String> columns;
	Vector<T> rows = new Vector<T>();
	TableListener instance;

	public CutomTableModel(TableListener instance) {
		super();
		this.instance = instance;

		this.columns = instance.getVectorColumns();
		this.columns.add(""); // for the buttons
		this.setColumnIdentifiers(this.columns);
	    
		//this.setRowCount(1);
	}
    
	@Override
	public void setValueAt(Object aValue, int row, int column) {
		super.setValueAt(aValue, row, column);

		// HashMap<String, Object> data = new HashMap<String, Object>();
		// for (int i = 0 ; i < columns.size() ; i++) {
		// data.put(key, value)
		// }
		TableListener t = rows.get(row);
		if (t != null)
			t.updateField(getColumnName(column), aValue);
	}
	
	public void setValueAt(T value, int row) {
		rows.set(row, value);
	}

	@Override
	public Object getValueAt(final int row, int column) {
		if (column == getColumnCount() - 1) {
				final JButton button = new JButton(new ImageIcon(getClass().getResource("/img/remove.png")));
				button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent action) {
						if (JOptionPane
								.showConfirmDialog(
										JOptionPane
												.getFrameForComponent(button),
										"Etes vous sér de vouloir supprimer la ligne ?\nL'action ne pourra pas étre annulée.",
										"Confirmation de suppression",
										JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
							removeRow(row);
							rows.remove(row);
						}
					}
				});
				return button;
		} else {
			// FIXME
			try {
				return rows.get(row).getVectorData().get(column);
			} catch (Exception e) {
				return super.getValueAt(row, column);
			}
			// if (row != getRowCount() - 1 && column != getColumnCount() - 1) {
			// System.out.println(row + "  " + column);
			//				
			// } else {
			//				
			// }
		}
	}
	
	public void insertRow(int i, T row) {
		rows.add(i, row);
		insertRow(i, row.getVectorData());
	}
	
	public void insertRow(T row) {
		insertRow(getRowCount(), row);
	}

	public Vector<T> getData() {
		return rows;
	}

	public void setData(List<T> data) {
		try {
			for (Iterator<T> iterator = data.iterator(); iterator.hasNext();) {
				T t = (T) iterator.next();
				insertRow(0, t);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
